package com.jams.goodox.activity.qa;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.login.RegistActivity;
import com.jams.goodox.activity.login.RoleChooseActivity;
import com.jams.goodox.activity.login.RoleInfoUploadActivity;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;

import org.json.JSONObject;

import butterknife.Bind;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by HP on 2018/5/21.
 * api/course/introduce
 */

public class CourseDetailActivity extends BaseActivity {


    private static final String TAG = "CourseDetailActivity";
    @Bind(R.id.headImage)
    ImageView headImage;

    @Bind(R.id.teacherInfo)
    TextView teacherInfo;

    @Bind(R.id.courseInfo)
    TextView courseInfo;

    int courseId;

    @Override
    protected int getLayoutId() {
        return R.layout.coursedetail_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "课程介绍");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        courseId = getIntent().getIntExtra("courseId",-1);

//        Glide.with(RoleInfoUploadActivity.this).load(filePath)
//                .placeholder(R.drawable.loading) //占位符 也就是加载中的图片，可放个gif
//                .error(R.mipmap.morentouxiang60) //失败图片
//                .bitmapTransform(new GlideCircleTransform(RoleInfoUploadActivity.this)).crossFade(1000).into(headImage);

    }

    @Override
    protected void onResume() {
        super.onResume();

        getCourseInfo();
    }

    private void getCourseInfo() {

        clearMap();
        params.put("requestData", "{" + "courseId:" + courseId + "}");

        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);

        showLoading();

        Call<ResponseBody> example = createCallService(LoginService.class).courseIntroduce(body);

        example.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                misLoading();
                try {
                    String data = response.body().string();
                    Log.d(TAG, "getCourseInfo data = " + data);

                    JSONObject jsonObject = new JSONObject(data);

                    if(200 == jsonObject.getInt("code")){
                        JSONObject object = new JSONObject(jsonObject.getString("responseData"));
                        teacherInfo.setText(object.getString("nickName")+
                        "," + object.getString("individualitySign"));

                        courseInfo.setText(object.getString("courseDescr"));

                        Glide.with(CourseDetailActivity.this).load(object.getString("imgPath"))
                                .placeholder(R.drawable.loading) //占位符 也就是加载中的图片，可放个gif
                                .error(R.mipmap.morentouxiang60) //失败图片
                                .bitmapTransform(
                                        new GlideCircleTransform(CourseDetailActivity.this)).crossFade(1000).into(headImage);


                    }else {
                        if(jsonObject.has("error")){
                            ToastUtil.show("error:" + jsonObject.getString("error") );
                        }else {
                            ToastUtil.show("网络异常！" );
                        }
                    }
                } catch (Exception e) {
                    ToastUtil.show("Exception:" + e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                Log.d(TAG,"onFailure = " + t.getMessage());
            }
        });
    }
}
